//
//  SharedViewCell.m
//  time
//
//  Created by bingo on 2018/5/20.
//  Copyright © 2018年 1ge. All rights reserved.
//

#import "SharedViewCell.h"
#import "HomeCollectionViewCell.h"
#import <Masonry/Masonry.h>
#import "NovelViewController.h"

static NSString *kHomeCollectionViewCell = @"HomeCollectionViewCell.h";

@interface SharedViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation SharedViewCell
#pragma mark-
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupUI];
        [self setupLayout];
    }
    return self;
}

- (void)setDataSource:(NSArray<CommonModel *> *)dataSource {
    
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)setupUI {
    UICollectionViewFlowLayout *collectionLayout = [UICollectionViewFlowLayout new];
    collectionLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionLayout.minimumInteritemSpacing = 10;//横向item间距(最小值)
    collectionLayout.minimumLineSpacing = 10;//行间距
    
    collectionLayout.itemSize = CGSizeMake(110, 176);//每个UICollectionViewCell 的大小
    collectionLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);//每个UICollectionViewCell 的 margin
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionLayout];
    self.collectionView.collectionViewLayout = collectionLayout;
    
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.clipsToBounds = YES;
    
    [self.collectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:kHomeCollectionViewCell];
    
}
- (void)setupLayout {
    [self.contentView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}

#pragma mark UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomeCollectionViewCell *cell = (HomeCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kHomeCollectionViewCell forIndexPath:indexPath];
    CommonModel *model = [self.dataSource objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.title;
    
    if ([model.imageUrl containsString:@"http:"]) {
        [cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"飞艇"]];
    } else {
        [cell.itemImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,model.imageUrl]] placeholderImage:[UIImage imageNamed:@"飞艇"]];
    }

    
    return cell;
}

#pragma mark UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NovelViewController *novel = [[NovelViewController alloc] init];
    novel.curModel = [self.dataSource objectAtIndex:indexPath.item];
    [[BaseViewController topViewController].navigationController pushViewController:novel animated:YES];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
